package com.huiqiao.lesson6.di;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AutowiredBean {

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "AutowiredBean{" +
                "name='" + name + '\'' +
                '}';
    }


    private AutowiredUserBean userBean;

    public AutowiredBean() {
        System.out.println("bean初始化，使用了AutowiredBean()方法, 且userBean = " + userBean + ", name = " + name);
    }

    public AutowiredBean(AutowiredUserBean userBean) {
        this.userBean = userBean;
        System.out.println("bean初始化，使用了AutowiredBean(AutowiredUserBean userBean)方法，且userBean = " + userBean + ", name = " + name);
    }

//    @Autowired
    public AutowiredBean(@Autowired(required = false) String name, AutowiredUserBean userBean) {
        this.name = name;
        this.userBean = userBean;
        System.out.println("bean初始化，使用了AutowiredBean(String name, AutowiredUserBean userBean)方法，且userBean = " + userBean + ", name = " + name);
    }

    @Autowired
    public void setUserBean(AutowiredUserBean userBean) {
        System.out.println("在这之前，userBean = " + this.userBean);
        this.userBean = userBean;
        System.out.println("在这之后，userBean = " + this.userBean);
    }
}
